package com.mycompany.sigemp.web;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.mycompany.sigemp.model.Cliente;
import com.mycompany.sigemp.service.ClienteService;

@Controller
public class ClienteController {
	@Autowired
	ClienteService cService;
	
	@RequestMapping(value="/formbuscar.htm", method=RequestMethod.GET)
	public String vistaBuscarCliente(){
		return "viewBuscarCliente";
	}
	
	@RequestMapping(value="/formbuscar.htm",method=RequestMethod.POST)
	public String procesarBusqueda(@RequestParam String txtCodigo, Model model){
		if(txtCodigo.equals("")){
			return "viewBuscarCliente";
		}
		Cliente cliente = cService.obtenerClientePorCodigo(txtCodigo); 
		if(cliente == null){
			return "viewBuscarCliente";
		}
		model.addAttribute( "cliente" , cliente);
		return "viewDetalleCliente";
	}
	
	@RequestMapping(value="/formregistrar.htm", method=RequestMethod.GET)
	public String vistaRegistrar(Model model){
		model.addAttribute(new Cliente());
		return "viewRegistrarCliente";
	}
	
	@RequestMapping(value="/formregistrar.htm",method=RequestMethod.POST)
	public String procesarRegistroDelCliente(@Valid Cliente cliente, BindingResult result, Model model){
		if (result.hasErrors()) {
			return "viewRegistrarCliente";
		}
		cService.resgistrarUnCliente(cliente);
		model.addAttribute(new Cliente());
		return "viewRegistrarCliente";
	}
	
	@RequestMapping(value = "/update.htm", method = RequestMethod.GET)
    public String verParaActualizar(@RequestParam String var, Model model){
        Cliente c = cService.obtenerClientePorCodigo(var); 
        model.addAttribute( "cliente" , c);     
        return "viewActualizarCliente";
    }
	
	@RequestMapping(value="/update.htm",method=RequestMethod.POST)
	public String actualizarDatosDelCliente(@Valid Cliente cliente, BindingResult result, Model model){
		if (result.hasErrors()) {
			return "viewActualizarCliente";
		}
		cService.actualizarCliente(cliente);
		Cliente cl = cService.obtenerClientePorCodigo(cliente.getCodigo()); 
		model.addAttribute( "cliente" , cl);
		return "viewDetalleCliente";	
	}
	
	@RequestMapping(value = "/eliminarv.htm", method = RequestMethod.GET)
    public String verParaEliminar(@RequestParam String var, Model model){
        Cliente c = cService.obtenerClientePorCodigo(var); 
        model.addAttribute( "cliente" , c);     
        return "viewEliminarCliente";
    }
	
	@RequestMapping(value="/eliminarv.htm",method=RequestMethod.POST)
	public String eliminarCliente(@Valid Cliente cliente, BindingResult result, Model model){
		if (result.hasErrors()) {
			return "viewEliminarCliente";
		}
		cService.eliminarUnCliente(cliente);
		return "viewBuscarCliente";	
	}
	

}
